﻿/*
using Game;

using Engine;
using Engine.Graphics;

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Diagnostics;
using System.Threading.Tasks;

using RoslynForSC;

namespace ApiToolkit
{
	public class CSharpScriptBlock : CubeBlock //方块定义
	{
		public const int Index = 1001;
		
		public override void Initialize()
		{
			base.Initialize();
			m_texture = ContentManager.Get<Texture2D>(texturePath);
		}
		public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawCubeBlock(primitivesRenderer, value, new Vector3(size), ref matrix, color, color, environmentData, m_texture);
        }
		public Texture2D m_texture;
		string texturePath = "Textures/BlocksDataEditor";
	}
	public class CSharpScriptBlockBehavior : SubsystemBlockBehavior
	{
		public override int[] HandledBlocks => new int[] { 1001 };
		
		public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner miner)
		{
			DialogsManager.ShowDialog(miner.ComponentPlayer.GuiWidget, new CSharpScriptEditingDialog(miner));
			return true;
		}
	}
	public class CSharpScriptEditingDialog : Dialog
	{
		ComponentMiner Miner;
		#region MemberWidgets
		TextBoxWidget CodeEditor;
		BevelledButtonWidget CloseButton;
		BevelledButtonWidget RunButton;
		ListPanelWidget MessageList;
		#endregion
		
		const string scriptClassName = "ScriptClass";
		const string scriptMethodName = "Run";
		
		private static string sourceCode = @"
using Game;

namespace CSharpScript_Survivalcraft
{
	public class " + scriptClassName + @"
	{
		public static void " + scriptMethodName + @"(ComponentMiner miner)
		{
			" + "Log.Information(\"Hello world!\");" + @"
		}
		
		// miner参数可以省略
		//public static void " + scriptMethodName + @"()
		//{
		//	" + "Log.Information(\"Hello world!\");" + @"
		//}
		//
	}
}";

		public CSharpScriptEditingDialog(ComponentMiner miner)
		{
			Miner = miner;
			XElement dialogNode = ContentManager.Get<XElement>("Dialogs/CSharpScriptEditingDialog");
			LoadContents(this, dialogNode);
			
			CodeEditor = Children.Find<TextBoxWidget>("CodeEditor");
			CodeEditor.Text = sourceCode;
			CloseButton = Children.Find<BevelledButtonWidget>("CloseButton");
			RunButton = Children.Find<BevelledButtonWidget>("RunButton");
			MessageList = Children.Find<ListPanelWidget>("MessageList");
			MessageList.ItemWidgetFactory = (item) =>
			{
				var diagnostic = (Message)item;
				Color textColor;
				switch(diagnostic.WarningLevel)
				{
					case 0:
						textColor = Color.White;
						break;
					case 1:
						textColor = Color.Yellow;
						break;
					case 2:
						textColor = Color.Red;
						break;
					case 3:
						textColor = Color.DarkRed;
						break;
					default:
						textColor = Color.Blue;
						break;
				}
				return new LabelWidget()
				{
					Text = diagnostic.ToString(),
					Color = textColor
				};
			};
			
		}
		public override void Update()
		{
			if(RunButton.IsClicked)
			{
				Run();
			}
			if(CloseButton.IsClicked)
			{
				Close();
			}
			if(sourceCode != CodeEditor.Text)
			{
				sourceCode = CodeEditor.Text;
			}
		}
		private void Close()
		{
			DialogsManager.HideDialog(this);
		}
		private async void Run()
		{
			MessageList.ClearItems();
			var result = await Task.Run( () => RoslynForSC.Roslyn.Compile(sourceCode) );
			if(result.Diagnostics.Length != 0)
			{
				foreach(var diagnostic in result.Diagnostics)
				{
					MessageList.AddItem(diagnostic);
				}
				return;
			}
			var asm = result.Assembly;
			
			var scriptClass = asm.GetType(scriptClassName);
			
			if(scriptClass == null) return;
				
			var methods = scriptClass.GetMethods(BindingFlags.Public | BindingFlags.Static);
			
			MethodInfo scriptMethod = null;
			foreach(var method in methods)
			{
				if(method.Name == scriptMethodName && (method.GetParameters().Count() == 0 || (method.GetParameters().Count() == 1 && method.GetParameters().First().ParameterType == typeof(ComponentMiner))))
				{
					scriptMethod = method;
				}
			}
			if(scriptMethod != null)
			{
				switch(scriptMethod.GetParameters().Length)
				{
					case 0:
						scriptMethod.Invoke(null, null);
						break;
					case 1:
						scriptMethod.Invoke(null, new object[]{ Miner });
						break;
				}
			}
		}
	}
}
*/